package org.gw.building.util;

import com.jcraft.jsch.JSch;
import com.jcraft.jsch.Session;
import org.springframework.beans.factory.InitializingBean;

public class SSHBridge implements InitializingBean, Runnable {

	private Session session;

	@Override
	public void afterPropertiesSet() throws Exception {
		String user = "root";// SSH连接用户名
		String password = "Zz123!@#";// SSH连接密码
		String host = "114.55.93.85";// SSH服务器
		int port = 22;// SSH访问端口

		try {
			JSch jsch = new JSch();
			session = jsch.getSession(user, host, port);
			session.setPassword(password);
			session.setConfig("StrictHostKeyChecking", "no");
			session.connect();
			System.out.println(session.getServerVersion());// 这里打印SSH服务器版本信息
			session.setPortForwardingL(3316, "127.0.0.1", 3306);
			Thread thread = new Thread(this);
			thread.setDaemon(true);
			thread.setName(this.getClass().getName());
			thread.start();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	@Override
	public void run() {
		while (true) {
			try {
				session.sendKeepAliveMsg();
				Thread.sleep(1000 * 10);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}

}
